package com.sicheng.algorithm;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * @author zsc
 * @version 1.0
 * @date 2021/12/24 8:36
 *
 * SELECT
 * TABLE_NAME  表名,
 * COLUMN_NAME  属性,
 * COLUMN_COMMENT 名称,
 * COLUMN_TYPE 数据类型,
 * COLUMN_KEY AS 主键,
 * COLUMN_COMMENT 注释,
 * IS_NULLABLE 是否可为空
 * FROM
 * INFORMATION_SCHEMA.COLUMNS
 * WHERE
 * table_schema ='数据库名';
 */
public class AlgoTest {

    public static void main(String[] args) {
        int n = 12;
        List<Integer> factor = factor(n);
        System.out.println(factor);
        dfs(factor, 0, n);
        System.out.println(res);
    }

    static Deque<Integer> path = new ArrayDeque<>();//正在搜索的路径
    static List<List<Integer>> res = new ArrayList<>();//最终的结果集

    public static List<Integer> factor(int n) {
        ArrayList<Integer> factor = new ArrayList<>();
        int c = (int) Math.sqrt(n);
        for (int i = 1; i <= c; i++) {
            if (n % i == 0) {
                factor.add(i);
                factor.add(n / i);
            }
        }
//        factor.sort(Integer::compareTo);
        factor.remove(0);
        return factor;
    }

    public static void dfs(List<Integer> factor, int start, int n) {

        if (n == 0)
            return;
        if (n == 1) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = start; i < factor.size(); i++) {
            if (n % factor.get(i) == 0){
                path.addLast(factor.get(i));
                dfs(factor, start, (n / factor.get(i)));
                path.removeLast();
            }
        }
    }
}
